﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Threading;

namespace MM.Common.Base
{
    public class RunExeFile
    {
        // Methods
        public void ReStart(string runPath, string uExeFile)
        {
            string path = null;
            Process process = new Process();
            Process[] processes = Process.GetProcesses();
            path = runPath + @"\" + uExeFile;
            string str2 = "";
            for (int i = 0; i < processes.Length; i++)
            {
                str2 = processes[i].ProcessName + ".exe";
                if (str2.ToLower() == uExeFile.ToLower())
                {
                    string str3 = "";
                    try
                    {
                        str3 = processes[i].MainModule.FileName.Substring(0, processes[i].MainModule.FileName.LastIndexOf(@"\")) + @"\" + str2 + ".CLS";
                    }
                    catch
                    {
                        break;
                    }
                    if (!File.Exists(str3))
                    {
                        FileStream stream = new FileStream(str3, FileMode.Append);
                        File.SetAttributes(str3, FileAttributes.Hidden);
                        stream.Close();
                    }
                    Thread.Sleep(0x5dc);
                    try
                    {
                        if (((processes[i] != null) && (processes[i].Handle != IntPtr.Zero)) && !processes[i].CloseMainWindow())
                        {
                            processes[i].Kill();
                        }
                        break;
                    }
                    catch
                    {
                        break;
                    }
                }
            }
            if (File.Exists(path))
            {
                process.StartInfo.FileName = path;
                process.Start();
            }
        }

        public void Run(string runPath, string uExeFile)
        {
            string path = "";
            Process process = new Process();
            Process[] processes = Process.GetProcesses();
            path = runPath + @"\" + uExeFile;
            for (int i = 0; i < processes.Length; i++)
            {
                if ((processes[i].ProcessName + ".exe").ToLower() == uExeFile.ToLower())
                {
                    return;
                }
            }
            if (File.Exists(path))
            {
                process.StartInfo.FileName = path;
                process.Start();
            }
        }
    }
}
